package mvc.framework;

import java.util.List;
import java.util.LinkedList;

/**
 * Implementazione parziale per i RequestHandler
 * 
 * @author Grillo
 */
public abstract class AbsRequestHandler implements IRequestHandler {
    public final static int SUCCESS = 0;
    
    protected DialogHandler dialogHandler = null;
    protected String nextPage = "";

    protected List<String> errors = new LinkedList<String>();
    
    public AbsRequestHandler(DialogHandler dialogHandler) {
        this.dialogHandler = dialogHandler;
        errors.add(""); // Questa e' la stringa per il codice SUCCESS
    }
    
    public String processRequest() {
        setNextPage();  // Impostazione della vista successiva
        
        int errorCode = validate();
        if (errorCode != SUCCESS) {
            if (((String) errors.get(errorCode)).equals("")) return nextPage;

            SessionManager.setErrorMessage(dialogHandler.getSession(), errors.get(errorCode));
        }
        else {
            errorCode = execute();
            if (errorCode != SUCCESS) {
                if (((String) errors.get(errorCode)).equals("")) return nextPage;

                SessionManager.setErrorMessage(dialogHandler.getSession(), errors.get(errorCode));
            }
        }
        return nextPage;
    }
    
    public abstract String getActionName();

    public abstract void setNextPage();

    public abstract IRequestHandler clone(DialogHandler dHandler) throws CloneNotSupportedException;

    public abstract int validate();

    public abstract int execute();
}